from appium import webdriver
import time
from appium.options.common import AppiumOptions
from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
option = AppiumOptions()
option.set_capability('platformName','android')
option.set_capability('platformVersion','7.1.2')
option.set_capability('deviceName','emulator-5554')
option.set_capability('appPackage','com.tencent.mm')
option.set_capability('appActivity','.ui.LauncherUI')
option.set_capability('noReset','true')
option.set_capability('newCommandTimeout','18000')
option.set_capability("recreateChromeDriverSessions",'true')
option.set_capability("chromedriverExecutable", 'D:\\chromedriver_92\\chromedriver.exe')
option.set_capability("browserName",'')
option.set_capability("sessionOverride",'true')
option.set_capability("setWebContentsDebuggingEnabled",'true')
option.set_capability("chromeOptions",{"androidProcess": "com.tencent.mm:appbrand0"})
# option.set_capability("chromeOptions",{"androidProcess":"com.tencent.mm:toolsmp"})
driver = webdriver.Remote(command_executor='http://127.0.0.1:4723/wd/hub',options=option)

# desired_caps['chromeOptions'] = {'androidProcess': 'com.tencent.mm:appbrand0'}
time.sleep(10)

print('微信往下滑')
loc = (AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().resourceId("com.tencent.mm:id/fe")')
WebDriverWait(driver, 10).until(EC.visibility_of_element_located(loc))


# 获得屏幕大小宽和高
def getSize(driver):
    x = driver.get_window_size()['width']
    y = driver.get_window_size()['height']
    return (x, y)


# 屏幕向上滑动
def swipeUp(driver, t=1000):
    l = getSize(driver)
    x1 = int(l[0] * 0.5)  # x坐标
    y1 = int(l[1] * 0.75)  # 起始y坐标
    y2 = int(l[1] * 0.25)  # 终点y坐标
    driver.swipe(x1, y1, x1, y2, t)


# 屏幕向下滑动
def swipeDown(driver, t=1000):
    l = getSize(driver)
    x1 = int(l[0] * 0.5)  # x坐标
    y1 = int(l[1] * 0.25)  # 起始y坐标
    y2 = int(l[1] * 0.75)  # 终点y坐标
    driver.swipe(x1, y1, x1, y2, t)


# 屏幕向左滑动
def swipLeft(driver, t):
    l = getSize(driver)
    x1 = int(l[0] * 0.75)
    y1 = int(l[1] * 0.5)
    x2 = int(l[0] * 0.05)
    driver.swipe(x1, y1, x2, y1, t)


# 屏幕向右滑动
def swipRight(driver, t=1000):
    l = getSize(driver)
    x1 = int(l[0] * 0.05)
    y1 = int(l[1] * 0.5)
    x2 = int(l[0] * 0.75)
    driver.swipe(x1, y1, x2, y1, t)


# 调用向下滑动的方法
swipeDown(driver)
time.sleep(2)
#点击小程序图标
driver.find_element(by=AppiumBy.ID, value="f15").click()
time.sleep(10)
print('点击小程序输入用户名输入框')
contexts = driver.contexts
print(contexts)
if 'WEBVIEW_com.tencent.mm:appbrand0' in contexts:
    print('切换到小程序上下文成功')
else:
    print('小程序上下文未找到')
time.sleep(15)
driver.switch_to.context("WEBVIEW_com.tencent.mm:appbrand0")
time.sleep(5)
# print('driver.current_window_handl--->',driver.current_window_handle)
handles = driver.window_handles
driver.switch_to_window(handles[1])
# e = driver.find_element(By.XPATH,'/html/body/wx-view/wx-view[3]/wx-view')
# print(e)
# print('driver.title----->',driver.title)
try:
    # 获取当前页面的HTML元素
    html_element = driver.page_source

    print('html_element=================================>>>>>>>>>',html_element)
    time.sleep(10)
    # 获取所有上下文
    contexts = driver.contexts
    print("所有上下文:", contexts)
    driver.implicitly_wait(30)
    # 切换到Webview上下文
    for context in contexts:
        if "WEBVIEW" in context:
            driver.switch_to.context(context)
            print("切换到上下文:", context)
            break
    # 获取输入框元素
    element = driver.find_element(By.XPATH, "/html/body/wx-view/wx-view[3]/wx-uni-easyinput[1]/wx-view/wx-view/wx-input/div[1]/input")
    # 输入文本
    print('element',element)
    element.send_keys('1234567890')
    # element = driver.find_element(By.XPATH, "//wx-input[1]")
    # print('driver.find_element(By.XPATH, "//wx-input[1]")---->',element)
    # element.send_keys('1234567890')
    # print('输入内容成功')
except TimeoutError:
    print('元素未找到')
# driver.find_element(by=AppiumBy.XPATH, value="/html/body/wx-view/wx-view[3]/wx-uni-easyinput[1]/wx-view/wx-view/wx-input/div[1]/input").click()
# time.sleep(10)
driver.quit()
